\chapter{Example}
\section{Motivation}
For this design lab, we are looking forward to have a clearly architecture demo which not only the entire student can easily getting started to the project, but also have the potential to extend it as a sophisticated project. Therefore, here I suggested LED - Light Sensor communication platform as a starting demo for this design lab. For my initial design, the whole system can been seen as the fundamental physical layer design of optic-communication. In this tutorial, I will introduce the basic self-loop design system, the advanced extension will be left for students.\\
There are actually five components in the system based on the Arduino. As illustrated in Fig.\ref{fig:sys}, 
\begin{itemize}
\item Data Source is to design for generate bit information.
\item Transmitter is to translate the bit information as the switch state of the LED.
\item Receiver is to read the light sensor's output.
\item Decision is to process the sensor's output, and make the derision for each bit information.
\item Evaluation is to compare the information between the source and destination and evaluate the performance.
\end{itemize}
The original design for this communication system can implement a loopback test. Your Arduino will generating bit data and translating it to the optic signal by LED. Then the light sensor will responded soon, the fluctuation resistance which contributed from the light sensitivity will been read by Arduino's ADC module. Finally, a proper simple process for these data will decode it as the bit information. In order to proof it meaningful and easy for compete to other, the evaluation module will evaluate the source information and received information. 
\begin{figure}[h]
\centering
\tikzstyle{block} = [rectangle, draw, fill=blue!20, text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex', ->]
\begin{tikzpicture}[node distance=3cm, line width=2pt]
    \node[block] (TX) at (0,0) {Transmitter};
    \node[block, right of = TX] (RX) {Receiver};
    \node[block, right of = RX] (DEC) {Decision};
    \node[block, left of = TX] (data) {Data Source};
    \node[block, below =1cm of DEC] (eval) {Evaluation};
    \path[line] (data) -- (TX);
    \path[line] (TX) -- (RX);
    \path[line] (RX) -- (DEC);
    \path[line] (DEC) -- (eval);
    \path[line] (data) |- (eval);
\end{tikzpicture}
\caption{System Framework}
\label{fig:sys}
\end{figure}
\section{Circuit Design}
As a hardware system, proper periphery should been design for Arduino. In our system, only Transmitter, Receiver need circuit to support their functionality.
\subsection{Transmitter Design}
The data will been represented as the switch form of the LED. Therefore, a proper LED circuit is needed as showed in Fig.\ref{fig:led}. When Pin13 is set to the high, the LED will delight until it set to be low voltage.
\begin{figure}[h]
\centering
\tikz [circuit ee IEC]
{
\node (led1) [diode={light emitting={info=LED}},point down] at (0,1) {};
\node (R1) [resistor,ohm=330,point down] at (0,3) {};
\node (ground) [ground, point down] at (0,0) {};
\node (pin) [ resistor, inner sep = 3pt] at (-2,4) {Pin13};
\draw (R1) -| (led1);
\draw (led1) -| (ground);
\draw (pin) -| (R1);
}
\caption{LED Circuit}
\label{fig:led}
\end{figure}

\subsection{Receiver Design}
The light will be sensed by our receiver part. We use a light sensor resistor to illustrate whether or not we receive the signal. The light sensor have magic behaviour, when light is sensed surround it, its resistor will decreased dramatically compare to the dark environment. Therefore using the Arduino's ADC we could sensing the voltage between the light resistor to demonstrate the signal received.
\begin{figure}[h]
\centering
\tikz [circuit ee IEC]
{
\draw[line width=2pt] (-0.5,5) -- +(1,0) node[above,pos=0.5]{5V};
\node (led1) [diode={light emitting={info=Light Sensor}},point down] at (0,1) {};
\node (R1) [resistor,ohm=1M,point down] at (0,3) {};
\node (ground) [ground, point down] at (0,0) {};
\node (pin) [ resistor, inner sep = 3pt] at (-2,2) {A0};
\draw (R1) -| (led1);
\draw (led1) -| (ground);
\draw (pin) -| (R1);
\draw (0,5) -- (R1);
}
\caption{Light Sensor Circuit}
\label{fig:light}
\end{figure}

\section{Software Design}
For the software part, there are five parts should we code. 
\subsection{Data Source}
In order to transmit bits and test our system, the source data should be generated randomly. In the \ref{code:source}, we use Arduino built-in function random() to generate pseudo-random number 0 and 1.
\lstset{language=C,caption={Source Data Generation}, label={code:source}}
\begin{lstlisting}
void gen_data()
{
  int i;  
  tx_buf.count = 0;
  rx_buf.count = 0;
  for (i=0; i < N ; i ++ )
    push(&tx_buf, random(2));   
}
\end{lstlisting} 

\subsection{Transmitter}
For transmitter part, the behaviour is pretty simple which is for each 0 and 1 bit to be transmitting from buffer, mapping it to the denoted TX pin.
\lstset{language=C,caption={Transmitter}, label={code:tx}}
\begin{lstlisting}
void tx_1()
{
  if(tx_buf.count!=0)
    digitalWrite(tx_pin, pop(&tx_buf)); 
}
\end{lstlisting} 

\subsection{Receiver}
The receiver is using the Arduino's ADC module. The received data plus noise will be pushed to our receiver buffer one by one in digital form.
\lstset{language=C,caption={Receiver}, label={code:rx}}
\begin{lstlisting}
int rx_2()
{
  return analogRead(A0);
}

void rx_1()
{
   if(rx_buf.count!=N)
  {
    push ( &rx_buf, rx_2() );
  }
}
\end{lstlisting} 

\subsection{Decision}
The decision part is nothing but batch of signal processing module. The observed transmitter signal plus noise is our target input. After some user-defined signal processing method, we are expect the bit information output. In the following example, I am using the trivial average threshold to make decision. For each N bit received signal, we firstly calculate their average value, and using it as the detection threshold.
\lstset{language=C,caption={Decision}, label={code:de}}
\begin{lstlisting}
void filter(struct BUF *buf)
{
  long ave = 0;
  int i;
  for(i=0; i<N; i++)
  {
    ave +=  buf->data[i] / N;
  }
  Serial.println("Decesion Line: \t");
  Serial.println(ave*0.0049, 4);
  for(i=0; i<N; i++)
  {
    buf->data[i] = buf->data[i] > ave;
  }
}
\end{lstlisting}
\subsection{Evaluation}
When restore the received signal, the next step will be evaluate our performance by compare the received and transmitted signal. if everything is correct, light a led to let us know.
\lstset{language=C,caption={Evaluation}, label={code:eva}}
\begin{lstlisting}
void compare()
{
  int i,err=0;
  for (i = 0 ; i<N; i++)
  {
    if(tx_buf.data[i] != rx_buf.data[N-i-1])
      err++;
  }
  if(err)
    light(0x01);
  else
    light(0x02);
}
\end{lstlisting}
\subsection{Time Issue}
The most difference part for embedded practical application and software simulation is timing issue. Since we are dealing with the real time and hardware signal, we need to implement a real-time software to guarantee the logic and schedule which usually relied a timer tick synchronization. Let us review our system's timing,
\begin{figure}[h]
\centering
\tikzstyle{bin} = [draw, line width=2pt]
\tikzstyle{noise} = [draw, line width=1pt]
\begin{tikzpicture}
[
TXT/.style = {align = center}
]
\draw[->] (-1,0) -- (11,0);
\draw[->] (0,-4) -- (0, 1.5);
\path[bin] (0,0) -| (1,1);
\path[bin] (1,1) -| (2,0);
\path[bin] (2,0) -| (3,1);
\path[bin] (3,1) -| (4,0);
\path[bin] (4,0) -| (5,1);
\path[bin] (5,1) -| (6,0);
\path[bin] (6,0) -| (7,1);
\path[bin] (7,1) -| (8,0);
\path[bin] (8,0) -| (9,1);
\path[bin] (9,1) -| (10,0);
\draw[->] (-1,-2) -- (11,-2);
\path[bin,->] (0,-2) -- (0,-0.5);
\path[bin,->] (2,-2) -- (2,-0.5);
\path[bin,->] (4,-2) -- (4,-0.5);
\path[bin,->] (6,-2) -- (6,-0.5);
\path[bin,->] (8,-2) -- (8,-0.5);
\path[bin,->] (10,-2) -- (10,-0.5);
\draw[->] (-1,-4) -- (11,-4);
\path[bin,->] (1,-4) -- (1,-2.5);
\path[bin,->] (3,-4) -- (3,-2.5);
\path[bin,->] (5,-4) -- (5,-2.5);
\path[bin,->] (7,-4) -- (7,-2.5);
\path[bin,->] (9,-4) -- (9,-2.5);
\node[TXT] at (-1, 1) {system\\ tick};
\end{tikzpicture}
\end{figure}
\section{Performance}